package net.lainiao.manager.control;

import com.github.pagehelper.PageInfo;
import net.lainiao.base.model.ReptileTopic;
import net.lainiao.service.service.ReptileTopicService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

@Controller
@RequestMapping("/reptile/topic")
public class ReptileTopicControl {
    private String baseDir="/reptile/topic/";

    @Resource
    private ReptileTopicService reptileTopicService;


    @RequestMapping(value = "/index")
    public String index(ModelMap modelMap) {
        return index(modelMap, 1);
    }

    @RequestMapping(value = "/index/{pageIndex}")
    public String index(ModelMap modelMap, @PathVariable int pageIndex) {
        PageInfo<ReptileTopic> pageInfo = reptileTopicService.pageModes(pageIndex, 10);
        modelMap.put("models", pageInfo.getList());
        modelMap.put("pageIndex", pageIndex);
        modelMap.put("pageTotal", pageInfo.getPages());
        return baseDir+"index";
    }

    @RequestMapping(value = "/del/{id}")
    public String del(ModelMap modelMap, @PathVariable int id) {
        reptileTopicService.delModel(id);
        return "redirect:"+baseDir+"index";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(ModelMap modelMap){
        return baseDir+"add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(ModelMap modelMap,@ModelAttribute ReptileTopic model){
        if(reptileTopicService.saveModel(model)>0){
            return "redirect:"+baseDir+"index";
        }
        modelMap.put("mess","新增失败");
        return add(modelMap);
    }



    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public String edit(ModelMap modelMap,@PathVariable int id){
        ReptileTopic model=reptileTopicService.getModelById(id);
        modelMap.put("net.lainiao.zhihu.model",model);
        return baseDir+"edit";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(ModelMap modelMap,@ModelAttribute ReptileTopic model){
        if(reptileTopicService.saveModel(model)>0){
            return "redirect:"+baseDir+"index";
        }
        modelMap.put("mess","保存失败");
        return edit(modelMap,model.getId());
    }


}